package com.weigang.dynamic.datasource.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.weigang.dynamic.datasource.entity.TestUser;
import com.weigang.dynamic.datasource.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    // 使用主数据源
    @DS("master")
    public String getUserToMaster(Integer id) {
        TestUser userById = userMapper.getUserById(id);
        return userById.toString();
    }

    // 使用从数据源
    @DS("slave")
    public String getUserFromSlave(Integer id) {
        return userMapper.getUserById(id).toString();
    }
}
